package com.example.wechat;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.widget.TextView;

public class DiscoverFragment extends Fragment {
    private RecyclerView recyclerView;
    private DiscoverAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discover, container, false);
        initViews(view);
        return view;
    }

    private void initViews(View view) {
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        
        adapter = new DiscoverAdapter(item -> {
            switch (item) {
                case 0: // 朋友圈
                    startActivity(new Intent(getActivity(), MomentsActivity.class));
                    break;
                case 1: // 扫一扫
                    // TODO: 实现扫一扫功能
                    break;
                case 2: // 摇一摇
                    // TODO: 实现摇一摇功能
                    break;
                case 3: // 附近的人
                    // TODO: 实现附近的人功能
                    break;
                case 4: // 游戏中心
                    // TODO: 实现游戏中心功能
                    break;
            }
        });
        recyclerView.setAdapter(adapter);
    }

    private static class DiscoverAdapter extends RecyclerView.Adapter<DiscoverAdapter.ViewHolder> {
        private final String[] items = {"朋友圈", "扫一扫", "摇一摇", "附近的人", "游戏中心"};
        private final OnItemClickListener listener;

        interface OnItemClickListener {
            void onItemClick(int position);
        }

        DiscoverAdapter(OnItemClickListener listener) {
            this.listener = listener;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(android.R.layout.simple_list_item_1, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.textView.setText(items[position]);
            holder.itemView.setOnClickListener(v -> listener.onItemClick(position));
        }

        @Override
        public int getItemCount() {
            return items.length;
        }

        static class ViewHolder extends RecyclerView.ViewHolder {
            TextView textView;

            ViewHolder(View itemView) {
                super(itemView);
                textView = itemView.findViewById(android.R.id.text1);
            }
        }
    }
} 